package com.yy.young.business.service.impl;

import com.yy.young.business.service.IProjectService;
import com.yy.young.common.util.CommonUtil;
import com.yy.young.common.util.StringUtils;
import com.yy.young.dal.service.IDataAccessService;
import com.yy.young.dal.util.Page;
import com.yy.young.business.model.Project;
import com.yy.young.business.util.BusinessConstants;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 项目服务实现
 * Created by rookie on 2018-03-09.
 */
@Service("projectService")
public class ProjectServiceImpl implements IProjectService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(ProjectServiceImpl.class);

    //获取数据列表
    @Override
    public List<Project> getProjectList(Project project) throws Exception {
        return dataAccessService.getList(BusinessConstants.MAPPER.TB_PROJECT + ".getProjectList", project);
    }

    //获取数据列表(分页)
    @Override
    public List<Project> getProjectPage(Project project, Page page) throws Exception {
        return dataAccessService.getList(BusinessConstants.MAPPER.TB_PROJECT + ".getProjectList", project, page);
    }

    //查询单条
    @Override
    public Project getProject(String id) throws Exception {
        return (Project)dataAccessService.getObject(BusinessConstants.MAPPER.TB_PROJECT + ".getProjectById", id);
    }

    //修改
    @Override
    public int updateProject(Project project) throws Exception {
        project.setUpdateTime(new Date());
        return dataAccessService.update(BusinessConstants.MAPPER.TB_PROJECT + ".update", project);
    }

    //批量删除
    @Override
    public int deleteProject(String[] idArr) throws Exception {
        int i = 0;
        for(String id : idArr){
            i += this.deleteProject(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int deleteProject(String id) throws Exception {
        return dataAccessService.delete(BusinessConstants.MAPPER.TB_PROJECT + ".delete", id);
    }

    //插入
    @Override
    public int insertProject(Project project) throws Exception {
        if (StringUtils.isBlank(project.getId())){
            project.setId(CommonUtil.getUUID());
        }
        project.setCreateTime(new Date());
        project.setUpdateTime(new Date());
        return dataAccessService.insert(BusinessConstants.MAPPER.TB_PROJECT + ".insert", project);
    }

}